<?php
/**
 * @abstract All the resources have to be one word
 * 
 */


class User_AjaxController extends Clu_Controller_Action_Hvn
{
	public $m; //model

	public function init()
	{
		$this->m = new user_Model_Role();
		parent::init();
	}

	public function updateRoleNameAction()
	{
		$id = $this->_request->getParam('id'); 
		$newRoleName = $this->_request->getParam('name');
    	$sess = new Zend_Session_Namespace(CLU_NS);
			
		if ($this->_request->isXmlHttpRequest())
		{
		    $ret = array();
		    if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = t(414);
				$this->_helper->json->sendJson($ret);
				exit();
			}
			
		    try {
	           	$this->m->saveRaw(null, array('rname' => $newRoleName), $id, false /*no validation needed*/); 
			    $ret['success'] = true;
		    }
		    catch (Exception $e)
		    {
		    	$ret['success'] = false;
		    }
		    $ret['name'] = $newRoleName;
		    $ret['referrer'] = $_SERVER['HTTP_REFERER'];
			$this->_helper->json->sendJson($ret);
		}
	}

	public function deleteRoleAction()
	{
		$id = $this->_request->getParam('id');
		$sess = new Zend_Session_Namespace(CLU_NS);
		
		if ($this->_request->isXmlHttpRequest())
		{
			$ret = array();
			if (strpos ($sess->userPermissions, 'administer user') === false)
			{
				$ret['success'] = false;
				$ret['message'] = __("Not allowed");
				$this->_helper->json->sendJson($ret);
			}
			if ($this->m->deleteByPrimary(array($id)))
            {
            	$ret['success'] = true;
            }
            else
            	$ret['success'] = false;
            	 
			$this->_helper->json->sendJson($ret);
		}
	}
	/*
	public function loginAction()
	{
		$auth = Zend_Auth::getInstance();
		$sess = new Zend_Session_Namespace(CLU_NS);
		if ($this->_request->isXmlHttpRequest())
		{
			if ($auth->hasIdentity()) {
	        	if ($this->_request->getParam('forceLogout') == 1) //sometimes user wants to login as a different user
	        	{
	        		//back to anonymous
	                Zend_Auth::getInstance()->clearIdentity();
	                $sess->userPermissions = Zend_Registry::get('configuration')->anon->perms; //?
	        	}
	            else
	            {
	            	$ret['success'] = false;
	            }
	        }
	        $form = new user_Form_Login();
      	    $form->cluBuild();
	        $values = $form->getValues();
			$ret = array();
			if ($result = $this->authenticate($values, $auth, 'login'))
			{
				$ret['success'] = true;
			}
            else
            {
            	$ret['success'] = false;
			}
			$this->_helper->json->sendJson($ret);
		}
	}*/
	public function followAction()
	{
		$idToFollow = $this->_request->getParam('id'); 
		die($idToFollow);
	}
	
	public function getUserFollowshipAction()
	{
		$uid = $this->_request->getParam('uid');
        $auth = Zend_Auth::getInstance();
        $uIdentity = $auth->getIdentity();
		if (is_null($uIdentity))
		{
			$ret['code'] = -1;
			$ret['label'] = t(2000); //login to follow
		}
		else if ($uIdentity->id == $uid) 
		{
			$ret['code'] = -1;
		}
		else if ($uIdentity->id != $uid)
		{
//			die('dajkfdkfl');
			//require_once  (APP_PATH.'/modules/user/Model/UserFollowship.php');
			$userFollowshipModel = new user_Model_UserFollowship();
			$follows = $userFollowshipModel->findByPrimary($uIdentity->id, $uid);
			if (!$follows) //null
				$ret['follows'] = 0;
			else
				$ret['follows'] = 1;
			$followed = $userFollowshipModel->findByPrimary($uid, $uIdentity->id);
			if (!$followed) //null
				$ret['followed'] = 0;
			else
				$ret['followed'] = 1;
		}
		$this->_helper->json->sendJson($ret);
	}
	
	//TODO: check for captcha
	public function setUserFollowshipAction()
	{
		$uid = $this->_request->getParam('uid');
        $auth = Zend_Auth::getInstance();
        $uIdentity = $auth->getIdentity();
		if (is_null($uIdentity))
		{
			$ret['success'] = 0;
			$ret['message'] = t(2000); //login to follow
		}
		else if ($uIdentity->id == $uid) 
		{
			$ret['success'] = 0;
			$ret['message'] = "Can't follow yourself";
		}
		else if ($uIdentity->id != $uid)
		{
			//require_once  (APP_PATH.'/modules/user/Model/UserFollowship.php');
			//require_once (APP_PATH. "/modules/user/Model/User.php");
			$userModel = new user_Model_User();
			$userFollowshipModel = new user_Model_UserFollowship();
			$followship = $this->_request->getParam('followship');
			
			$temp = $userModel->findByPrimary($uid);
			if ($followship == 1)
			{
				try 
				{
    				$userFollowshipModel->insert(array('uid1' => $uIdentity->id, 'uid2' => $uid, 'uid1_name' => $uIdentity->name, 'uid2_name' => $temp['name']));
    				//TODO: update content list here
    				$ret['success']  = 1;
				}
				catch (Exception $e)
				{
					$ret['success']  = 0;
					$ret['message'] = "Failed following";
				}
			}
			elseif ($followship == 0)
			{
				$db = Zend_Db_Table::getDefaultAdapter();
				$sql = "DELETE FROM user_followship WHERE uid1 = ". $db->quote($uIdentity->id, 'INTEGER') ." AND uid2=" . $db->quote($uid, 'INTEGER') ;
				try 
				{
    				$db->query($sql);
    				//TODO: update content list here    				
    				$ret['success']  = 2;
				}
				catch (Exception $e)
				{
					$ret['success']  = 0;
					$ret['message'] = "Failed unfollowing";
				}
			}
			
			
		}
		$this->_helper->json->sendJson($ret);
	}	
	
	public function checkedUserNameAndEmailAction()
	{
		$data = $this->_request->getParams();
		if ($this->_request->isXmlHttpRequest())
		{
			if(isset($data['name']))
	        	$sql = "SELECT id FROM users WHERE name = " . $data['name'];
	        else if(isset($data['mail']))
	        	$sql = "SELECT id FROM users WHERE mail = " . $data['mail'];
	        
			$ret = array();
			if ($result = $this->getAdapter()->fetchRow($sql))
			{
				$ret['success'] = false;
			}
            else
            {
            	$ret['success'] = true;
			}
			$this->_helper->json->sendJson($ret);
		}
	}
	
}

